From 5d561e1e2dcde3c9fca4d925f12447009d0d4a4c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Denis-Courmont?= <remi@remlab.net>
Date: Wed, 18 Apr 2018 17:23:57 +0300
Subject: [PATCH] posix: remove ancient run-time fallback to real-time clock
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

posix: remove ancient run-time fallback to real-time clock

For hysterical raisins, GNU/Linux and possibly some other OSes still
report that monotonic clock must be checked at run-time, although I
doubt that VLC or even current glibc would run on such old kernel.

Drop that to simplify and avoid the systematic one-time init check.

Downloaded from upstream commit to fix build error on m68k:

posix/thread.c:79:5: warning: #warning Monotonic clock not available. Expect timing issues. [-Wcpp]
 #   warning Monotonic clock not available. Expect timing issues.
     ^~~~~~~
posix/thread.c: In function ‘vlc_clock_setup_once’:
posix/thread.c:88:18: error: lvalue required as left operand of assignment
     vlc_clock_id = (val < 0) ? CLOCK_REALTIME : CLOCK_MONOTONIC;

[Bernd: rebased for 3.0.19]
Signed-off-by: Bernd Kuhls <bernd.kuhls@t-online.de>
---
 src/posix/thread.c | 96 +++++++-----------------------------------------------
 1 file changed, 11 insertions(+), 85 deletions(-)

diff --git a/src/posix/thread.c b/src/posix/thread.c
index dab8b71f97..8878941913 100644
--- a/src/posix/thread.c
+++ b/src/posix/thread.c
@@ -51,62 +51,16 @@
 # include <sys/pset.h>
 #endif
 
-#if !defined (_POSIX_TIMERS)
-# define _POSIX_TIMERS (-1)
-#endif
-#if !defined (_POSIX_CLOCK_SELECTION)
-/* Clock selection was defined in 2001 and became mandatory in 2008. */
-# define _POSIX_CLOCK_SELECTION (-1)
-#endif
-#if !defined (_POSIX_MONOTONIC_CLOCK)
-# define _POSIX_MONOTONIC_CLOCK (-1)
-#endif
-
-#if (_POSIX_TIMERS > 0)
 static unsigned vlc_clock_prec;
 
-# if (_POSIX_MONOTONIC_CLOCK > 0) && (_POSIX_CLOCK_SELECTION > 0)
-/* Compile-time POSIX monotonic clock support */
-#  define vlc_clock_id (CLOCK_MONOTONIC)
-
-# elif (_POSIX_MONOTONIC_CLOCK == 0) && (_POSIX_CLOCK_SELECTION > 0)
-/* Run-time POSIX monotonic clock support (see clock_setup() below) */
-static clockid_t vlc_clock_id;
-
-# else
-/* No POSIX monotonic clock support */
-#   define vlc_clock_id (CLOCK_REALTIME)
-#   warning Monotonic clock not available. Expect timing issues.
-
-# endif /* _POSIX_MONOTONIC_CLOKC */
-
 static void vlc_clock_setup_once (void)
 {
-# if (_POSIX_MONOTONIC_CLOCK == 0)
-    long val = sysconf (_SC_MONOTONIC_CLOCK);
-    assert (val != 0);
-    vlc_clock_id = (val < 0) ? CLOCK_REALTIME : CLOCK_MONOTONIC;
-# endif
-
     struct timespec res;
-    if (unlikely(clock_getres (vlc_clock_id, &res) != 0 || res.tv_sec != 0))
+    if (unlikely(clock_getres(CLOCK_MONOTONIC, &res) != 0 || res.tv_sec != 0))
         abort ();
     vlc_clock_prec = (res.tv_nsec + 500) / 1000;
 }
 
-static pthread_once_t vlc_clock_once = PTHREAD_ONCE_INIT;
-
-# define vlc_clock_setup() \
-    pthread_once(&vlc_clock_once, vlc_clock_setup_once)
-
-#else /* _POSIX_TIMERS */
-
-# include <sys/time.h> /* gettimeofday() */
-
-# define vlc_clock_setup() (void)0
-# warning Monotonic clock not available. Expect timing issues.
-#endif /* _POSIX_TIMERS */
-
 static struct timespec mtime_to_ts (vlc_tick_t date)
 {
     lldiv_t d = lldiv (date, CLOCK_FREQ);
@@ -233,14 +187,11 @@ void vlc_cond_init (vlc_cond_t *p_condvar)
 {
     pthread_condattr_t attr;
 
-    if (unlikely(pthread_condattr_init (&attr)))
-        abort ();
-#if (_POSIX_CLOCK_SELECTION > 0)
-    vlc_clock_setup ();
-    pthread_condattr_setclock (&attr, vlc_clock_id);
-#endif
-    if (unlikely(pthread_cond_init (p_condvar, &attr)))
+    if (unlikely(pthread_condattr_init (&attr))
+     || unlikely(pthread_condattr_setclock(&attr, CLOCK_MONOTONIC))
+     || unlikely(pthread_cond_init (p_condvar, &attr)))
         abort ();
+
     pthread_condattr_destroy (&attr);
 }
 
@@ -625,44 +576,27 @@ void vlc_control_cancel (int cmd, ...)
 
 vlc_tick_t mdate (void)
 {
-#if (_POSIX_TIMERS > 0)
     struct timespec ts;
 
-    vlc_clock_setup ();
-    if (unlikely(clock_gettime (vlc_clock_id, &ts) != 0))
+    if (unlikely(clock_gettime(CLOCK_MONOTONIC, &ts) != 0))
         abort ();
 
     return (INT64_C(1000000) * ts.tv_sec) + (ts.tv_nsec / 1000);
-
-#else
-    struct timeval tv;
-
-    if (unlikely(gettimeofday (&tv, NULL) != 0))
-        abort ();
-    return (INT64_C(1000000) * tv.tv_sec) + tv.tv_usec;
-
-#endif
 }
 
 #undef mwait
 void mwait (vlc_tick_t deadline)
 {
-#if (_POSIX_CLOCK_SELECTION > 0)
-    vlc_clock_setup ();
+    static pthread_once_t vlc_clock_once = PTHREAD_ONCE_INIT;
+
     /* If the deadline is already elapsed, or within the clock precision,
      * do not even bother the system timer. */
+    pthread_once(&vlc_clock_once, vlc_clock_setup_once);
     deadline -= vlc_clock_prec;
 
     struct timespec ts = mtime_to_ts (deadline);
 
-    while (clock_nanosleep (vlc_clock_id, TIMER_ABSTIME, &ts, NULL) == EINTR);
-
-#else
-    deadline -= mdate ();
-    if (deadline > 0)
-        msleep (deadline);
-
-#endif
+    while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) == EINTR);
 }
 
 #undef msleep
@@ -670,15 +604,7 @@ void msleep (vlc_tick_t delay)
 {
     struct timespec ts = mtime_to_ts (delay);
 
-#if (_POSIX_CLOCK_SELECTION > 0)
-    vlc_clock_setup ();
-    while (clock_nanosleep (vlc_clock_id, 0, &ts, &ts) == EINTR);
-
-#else
-    while (nanosleep (&ts, &ts) == -1)
-        assert (errno == EINTR);
-
-#endif
+    while (clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &ts) == EINTR);
 }
 
 unsigned vlc_GetCPUCount(void)
-- 
2.14.4

